function leadTable(){

    $('#scoreTb').bootstrapTable({
        url:'/api/getbypagescore',
        striped:true,//设置为true会有隔行变色效果
        pagination:true,//设置为true会在底部显示分页条
        singleSelect: false,//设置为false将禁止多选
        pageSize:7,//如果设置了分页，每页数据条数
        pageList:[7],
        pageNumber:1,//如果设置了分页。首页页码
        sidePagination: "server",//设置在哪里进行分页，可选项为“client”
        responseHandler: function (res) {
            return{                            //return bootstrap-table能处理的数据格式
                "total":res.data.total,
                "rows":res.data.rows
            }
        },
        queryParams:function (params){
            let paraObj = {
                size:params.limit,
                page:params.offset/params.limit,
                sort:"sno",
                direct:"desc",
                cname:$("#searchCourseName").val(),
                score:$("#searchScore").val(),
                sname:$("#searchStudentName").val()
            };

            return paraObj;
        },
        columns:[{
            field:'id',
            title:'序号'
        },{
            field: 'cno',
            title: '课程编号'
        },{
            field: 'cname',
            title: '课程名称'
        },{
            field: 'sno',
            title: '学生学号'
        },{
            field: 'sname',
            title: '学生姓名'
        },{
            field: 'score',
            title: '成绩'
        },{
            title: '操作',
            width: '100px',
            formatter:function (data, row, meta){
                let operators = '<a href="#" class="btn btn-mint btn-xs btn-icon" onclick=editScore(' + row.id + ') title="title">编辑</a> ' +
                    '<a href="#" class="btn btn-mint btn-xs btn-icon" onclick=deleteScore(' + row.id + ') title="title">删除</a>';
                return operators;
            }
        }]
    });
}

function search(){
    $('#scoreTb').bootstrapTable("destroy");
    leadTable();
}

function resetsearch(){
    cname:$("#searchCourseName").val("");
    sname:$("#searchStudentName").val("");
    score:$("#searchScore").val("");
    search();
}


function saveScore(){
    let data = $("#scoreform").serialize();

    let id = $("#id").val();
    let requestObj = null;
    if(id < 1){
        requestObj = {
            method: 'post',
            url: '/api/scoreinsert',
            data:data
        }
    }
    else {
        //更新
        requestObj = {
            method: 'put',
            url: '/api/scoreupdate',
            data:data
        }
    }

    $.ajax(requestObj).done(function (){
        $('#scoreModel').modal('hide');
        $('#scoreTb').bootstrapTable("destroy");
        leadTable();
    });
}

function editScore(id){

    // $('#studentModel').modal('show');
    $.ajax({
        method:'GET',
        url:'/api/scoreget/'+ id
    }).done(function (rs){
        $('#id').val(rs.data.id);
        $('#cno').val(rs.data.cno);
        $('#cname').val(rs.data.cname);
        $('#sno').val(rs.data.sno);
        $('#sname').val(rs.data.sname);
        $('#score').val(rs.data.score);
        $('#scoreModel').modal('show');
    });
}

function preAdd(){
    $("#id").val(0);//提示新增
    $('#scoreModel').modal('show');

}

function deleteScore(id) {
    if (confirm("您是否真的要删除吗？别后悔哦！")) {
        $.ajax({
            method: 'delete',
            url: '/api/scoredelete/' + id
        }).done(function () {
            $('#scoreTb').bootstrapTable("destroy");
            leadTable();
        });
    }// end if
}

function clearForm(){
    $("#cno").val("");
    $("#cname").val("");
    $("#sno").val("");
    $("#sname").val("");
    $("#score").val("");
}

$(function () {
    // $.ajax({
    //     url:"/api/login/nginxlogin"
    // }).done(function (rs){
    //     if(rs >= 0){
    //         //成功
    //         leadTable();
    //     }else {
    //         window.location.href="login/user"
    //     }
    // });
    leadTable();
})

$('#scoreModel').on('hidden.bs.modal',clearForm)//清空代码